package com.example.asus.yuejian_10.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.asus.yuejian_10.Config;
import com.example.asus.yuejian_10.R;
import com.example.asus.yuejian_10.RetrofitSeveice;
import com.example.asus.yuejian_10.bean.Train;
import com.example.asus.yuejian_10.bean.TrainInfo;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.asus.yuejian_10.Config.IMAGE_URL;


public class TrainxqActivity extends AppCompatActivity {
    private ImageView train_pic;
    private TextView train_intro;
    private TextView train_adress;
    private TextView train_time;
    private Button train_apply;
    private Context context;
    private String id,name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trainxq);
        train_pic= (ImageView) findViewById(R.id.train_pic);
        train_intro= (TextView) findViewById(R.id.train_intro);
        train_adress= (TextView) findViewById(R.id.train_address);
        train_time= (TextView) findViewById(R.id.train_time);
        train_apply= (Button) findViewById(R.id.train_apply);

        Retrofit retrofit =
                new Retrofit.Builder()
                        .baseUrl(Config.URL)
                        .addConverterFactory(GsonConverterFactory.create()).build();
        RetrofitSeveice service = retrofit.create(RetrofitSeveice.class);

        id = getIntent().getStringExtra("trainid");
        //Log.v("id",id);

        Call<TrainInfo> call = service.getTraininfo("2ul9sj8vlfoZ122e1s98048k2","train",id);
        call.enqueue(new Callback<TrainInfo>() {
            @Override
            public void onResponse(Call<TrainInfo> call, Response<TrainInfo> response) {
                Log.v("状态","联网成功");
                TrainInfo trainInfo = response.body();
                name = trainInfo.getName();
                train_intro.setText(trainInfo.getIntro());
                train_time.setText(trainInfo.getTraintime());
                train_adress.setText(trainInfo.getPlace());
                Picasso.with(context)
                        .load(IMAGE_URL+trainInfo.getThumb())
                        .into(train_pic);
            }

            @Override
            public void onFailure(Call<TrainInfo> call, Throwable t) {
                Log.v("状态","联网失败");
            }
        });

        train_apply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(TrainxqActivity.this,ApplyActivity.class);
                intent.putExtra("trainid",id);
                intent.putExtra("trainname",name);
                startActivity(intent);
            }
        });

    }
}
